//
//  CTResetPasswordLogic.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/4/22.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTResetPasswordLogic.h"
#import "CTDebugManager.h"
#import "CTUrl.h"
#import "CTRequestUtil.h"
#import "CTHeader.h"
#import "CTFile.h"
#import "CTRequestErrorCode.h"


@implementation CTResetPasswordLogic


-(void)dealloc
{
    [CTDebugManager showDebugLogInfo:@"dealloc : CTResetPasswordLogic Is Removed!!!"];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}



-(void)resetPasswordRegisterRequest:(CTRestPasswordRequestItem *)item
           successCompletionHandler:(requestSuccessBlock)successBlock
                      failedHandler:(requestFailedBlock)failedBlock
{
    NSMutableDictionary * param = [NSMutableDictionary dictionaryWithCapacity:0];
    @try {
        [param setObject:item.mobile forKey:@"mobile"];
        [param setObject:item.cipher forKey:@"cipher"];
        
    }
    @catch (NSException *exception) {
        [CTDebugManager showErrorLogInfo:@"重置密码请求参数转换字典失败！！"];
    }
    
    [CTRequestUtil sendAsyPostRequest:CT_URL_ACCOUNTS_FORGET(version) withParam:param withCompletionSuccessHandler:^(NSData *data) {
       
        BOOL isParserSuccess = [self parserData:data];
        if (isParserSuccess == YES) {
            [[NSNotificationCenter defaultCenter] postNotificationName:CTLoginSuccessNotification object:nil];
            successBlock();
        }
        else
        {
            [[NSNotificationCenter defaultCenter] postNotificationName:CTLoginFailedNotification object:nil];
            failedBlock();
        }
        
        
    } withFailedHandler:^(NSError *error) {
        [[NSNotificationCenter defaultCenter] postNotificationName:CTLoginFailedNotification object:nil];
        failedBlock();
    }];
}



#pragma mark - data parser -

-(BOOL)parserData:(NSData *)resultData
{
    
    BOOL isParserSuccess = NO;
    
    @try {
        
        NSError * error;
        
        id jsonObject = [NSJSONSerialization
                         JSONObjectWithData:resultData
                         options:NSJSONReadingAllowFragments
                         error:&error];
        if (jsonObject != nil && error == nil){
            
            if ([jsonObject isKindOfClass:[NSDictionary class]]){
                
                NSDictionary *deserializedDictionary = (NSDictionary *)jsonObject;
                
                NSString * errCode = [NSString stringWithFormat:@"%@",(NSString *)[deserializedDictionary objectForKey:@"error_code"]];
                if ([errCode isEqualToString:@"0"]) {
                    
                    isParserSuccess = YES;
                    
                    NSDictionary * infoDic = [deserializedDictionary objectForKey:@"info"];
                    NSString * mobileStr = [NSString stringWithFormat:@"%@",[infoDic objectForKey:@"mobile"]];
                    
                    //user phonenumber
                    [CTFile cacheFile:mobileStr
                             fileName:@"userPhoneNumber.text"
                              saveDoc:CTFileDocDocuments
                             savePath:@"user"];
                    
                }else {
                    NSString * errDescription = [CTRequestErrorCode errDescription:errCode];
                    [CTDebugManager showErrorLogInfo:errDescription];
                }
            }
        }
        else
        {
            [CTDebugManager showErrorLogInfo:@"解析手机号修改数据错误！"];
        }
    }
    @catch (NSException *exception) {
        [CTDebugManager showErrorLogInfo:@"解析手机号修改数据错误！"];
    }
    
    return isParserSuccess;
    
}


@end
